package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.my.vote.R;
import com.my.vote.entity.req.ReqRegister;
import com.my.vote.pst.user.IPstRegister;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;
import com.sfh.lib.ui.DialogView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述:注册
 *
 * @author
 * @date 2018/4/11
 */
public class RegisterActivity extends AbstractActivity implements IPstRegister.IVRegister {
    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.btLogin)
    Button btLogin;
    @BindView(R.id.cbSave)
    CheckBox cbSave;

    IPstRegister pstRegister;

    public static void startUI(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        tvLeft.setText("Register");
        tvRight.setText("Existing Account");
    }

    @OnClick({R.id.tvLeft, R.id.tvRight, R.id.btLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLeft:
                finish();
                break;
            case R.id.tvRight:
                LoginActivity.startUI(RegisterActivity.this, 0);
                finish();
                break;
            case R.id.btLogin:

                if (pstRegister == null) {
                    return;
                }
                ReqRegister reqRegister = new ReqRegister();
                reqRegister.setAgree(cbSave.isChecked());
                reqRegister.setNumber(etName.getText().toString());
                reqRegister.setPwd(etPwd.getText().toString());
                pstRegister.register(reqRegister);
                break;
        }
    }

    @Override
    public void onSuccess() {
        DialogView dialogView = new DialogView.Builder().setTitle("Prompt").setRightText("OK").setCancele(false).setMessage("login was successful").setShowLeft(false).setRightListener(new DialogView.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogFragment dialogFragment, int i) {
                dialogFragment.dismiss();
                finish();
            }
        }).build();
        showDialog(dialogView);
    }

    @Override
    public void onError(String error) {
        // 失败提示
        showToast(error,2);
    }

    @Override
    public IPresenter getPresenter() {
        if (pstRegister == null) {
            pstRegister = LoaderService.getService(IPstRegister.class);
        }
        return pstRegister;
    }
}
